import {
	Controller,
	Get,
	Post,
	Body,
	Patch,
	Param,
	Delete,
	Query,
} from "@nestjs/common";
import { OrderService } from "./order.service";
import { CreateOrderDto } from "./dto/create-order.dto";
import { UpdateOrderDto } from "./dto/update-order.dto";
import { Order } from "./entities/order.entity";

@Controller("orders")
export class OrderController {
	constructor(private readonly orderService: OrderService) {}

	@Post("create")
	async create(
		@Body() createOrderDto: CreateOrderDto
	): Promise<{ code: number; data: Order; message: string }> {
		const order = await this.orderService.create(createOrderDto);
		return {
			code: 200,
			data: order,
			message: "订单创建成功",
		};
	}

	@Get("test")
	async test(): Promise<{ code: number; data: any; message: string }> {
		const orderItems = await this.orderService.getOrderItems();
		return {
			code: 200,
			data: orderItems,
			message: "获取订单项测试数据成功",
		};
	}

	@Get("list")
	async findAll(@Query() query): Promise<{
		code: number;
		data: { list: Order[]; total: number; page: number; limit: number };
		message: string;
	}> {
		const [orders, total] = await this.orderService.findAll(query);
		return {
			code: 200,
			data: {
				list: orders,
				total,
				page: +query.page || 1,
				limit: +query.limit || 10,
			},
			message: "获取订单列表成功",
		};
	}

	@Get("user/:userId")
	async findByUserId(
		@Param("userId") userId: string,
		@Query() query
	): Promise<{
		code: number;
		data: { list: Order[]; total: number; page: number; limit: number };
		message: string;
	}> {
		const [orders, total] = await this.orderService.findByUserId(
			+userId,
			query
		);
		return {
			code: 200,
			data: {
				list: orders,
				total,
				page: +query.page || 1,
				limit: +query.limit || 10,
			},
			message: "获取用户订单列表成功",
		};
	}

	@Get("findOne/:id")
	async findOne(
		@Param("id") id: string
	): Promise<{ code: number; data: Order; message: string }> {
		const order = await this.orderService.findOne(+id);
		return {
			code: 200,
			data: order,
			message: "获取订单详情成功",
		};
	}

	@Patch("update/:id")
	async update(
		@Param("id") id: string,
		@Body() updateOrderDto: UpdateOrderDto
	): Promise<{ code: number; data: Order; message: string }> {
		const order = await this.orderService.update(+id, updateOrderDto);
		return {
			code: 200,
			data: order,
			message: "订单更新成功",
		};
	}

	@Delete("delete/:id")
	async remove(
		@Param("id") id: string
	): Promise<{ code: number; message: string }> {
		await this.orderService.remove(+id);
		return {
			code: 200,
			message: "订单删除成功",
		};
	}

	@Post(":id/pay")
	async pay(
		@Param("id") id: string
	): Promise<{ code: number; data: Order; message: string }> {
		const updateOrderDto: UpdateOrderDto = { status: "待发货" };
		const order = await this.orderService.update(+id, updateOrderDto);
		return {
			code: 200,
			data: order,
			message: "订单支付成功",
		};
	}

	@Post(":id/confirm")
	async confirm(
		@Param("id") id: string
	): Promise<{ code: number; data: Order; message: string }> {
		const updateOrderDto: UpdateOrderDto = { status: "已完成" };
		const order = await this.orderService.update(+id, updateOrderDto);
		return {
			code: 200,
			data: order,
			message: "确认收货成功",
		};
	}

	@Post(":id/cancel")
	async cancel(
		@Param("id") id: string
	): Promise<{ code: number; data: Order; message: string }> {
		const order = await this.orderService.findOne(+id);
		let message = "取消订单成功";

		if (order.status === "待发货") {
			message = "取消订单成功，积分已退还";
		}

		const updateOrderDto: UpdateOrderDto = { status: "已取消" };
		const updatedOrder = await this.orderService.update(+id, updateOrderDto);

		return {
			code: 200,
			data: updatedOrder,
			message,
		};
	}
}
